﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CuaHangLaptop.BIZ;

namespace QuanLy.GUI
{
    public partial class CTHoaDon_GUI : Form
    {
        List<CTHoaDon_BIZ> list_CTHD = new List<CTHoaDon_BIZ>();
        List<Laptop_BIZ> list_lap = new List<Laptop_BIZ>();
        CTHoaDon_BIZ current_cthd = new CTHoaDon_BIZ();
        HoaDon_BIZ hoadon = new HoaDon_BIZ();
        bool updated = true;

        public CTHoaDon_GUI(HoaDon_BIZ _hd)
        {
            InitializeComponent();
            hoadon = _hd;
            lb_maHD.Text += " " + hoadon.MaHoaDon;
            lb_tenKH.Text += " " + hoadon.KhachHang.TenKH;
            GridView_CTHD.AutoGenerateColumns = false;
            Frm_Load();
        }

        void Frm_Load()
        {
            list_CTHD = current_cthd.GetAll_CTHoaDon_ByHD(hoadon.MaHoaDon);
            list_lap = current_cthd.Laptop.GetAll_Laptop();
            GridView_CTHD.DataSource = list_CTHD;
            cb_laptop.DataSource = list_lap;
            cb_laptop.ValueMember = "MaLaptop";
            cb_laptop.DisplayMember = "TenLaptop";
            Disable_All(true);
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            Reset();
            Disable_All(false);
            updated = false;
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            try
            {
                int tonkho = 0;
                bool thieu_sp = false;
                if (!updated)
                {
                    current_cthd = new CTHoaDon_BIZ(-1, txt_MaCTHD.Text, hoadon.MaHoaDon, cb_laptop.SelectedValue.ToString(), int.Parse(txt_soluong.Text));
                    tonkho = current_cthd.Ktra_TonKho(hoadon.NgayLap);
                    if (int.Parse(txt_soluong.Text) > tonkho)
                    {
                        thieu_sp = true;
                        MessageBox.Show("Số lượng hàng trong kho không đủ!");
                    }
                    else list_CTHD.Add(current_cthd);
                }
                else
                {
                    tonkho = current_cthd.Ktra_TonKho(hoadon.NgayLap);
                    if (int.Parse(txt_soluong.Text) > tonkho)
                    {
                        thieu_sp = true;
                        MessageBox.Show("Số lượng hàng trong kho không đủ!");
                    }
                    else Get_Val();
                }
                if (!thieu_sp)
                {
                    current_cthd.Save();
                    GridView_CTHD.DataSource = null;
                    GridView_CTHD.DataSource = list_CTHD;
                    Disable_All(true);
                    updated = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi! " + ex.Message);
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GridView_HD_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GridView_CTHD.Columns[e.ColumnIndex].HeaderText == "Xóa")
            {
                current_cthd = (CTHoaDon_BIZ)GridView_CTHD.Rows[e.RowIndex].DataBoundItem;
                list_CTHD.Remove(current_cthd);
                current_cthd.Delete();
                GridView_CTHD.DataSource = null;
                GridView_CTHD.DataSource = list_CTHD;
            }
            if (GridView_CTHD.Columns[e.ColumnIndex].HeaderText == "Sửa")
            {
                current_cthd = (CTHoaDon_BIZ)GridView_CTHD.Rows[e.RowIndex].DataBoundItem;
                Disable_All(false);
            }
        }

        private void GridView_HD_SelectionChanged(object sender, EventArgs e)
        {
            if (GridView_CTHD.SelectedRows.Count > 0)
            {
                current_cthd = (CTHoaDon_BIZ)GridView_CTHD.SelectedRows[0].DataBoundItem;
                Set_Val();
                Disable_All(true);
            }
        }

        void Reset()
        {
            int id = current_cthd.Get_Lasted_ID();
            txt_MaCTHD.Text = "CTHD"+(id+1);
            txt_soluong.Text = "0";
            cb_laptop.SelectedIndex = 0;
        }

        void Get_Val()
        {
            current_cthd.Laptop = current_cthd.Laptop.Get_Laptop_ByCode(cb_laptop.SelectedValue.ToString());
            current_cthd.MaCThoadon = txt_MaCTHD.Text;
            current_cthd.Soluong = int.Parse(txt_soluong.Text);
        }

        void Set_Val()
        {
            cb_laptop.SelectedValue = current_cthd.Laptop.MaLaptop;
            txt_soluong.Text= current_cthd.Soluong.ToString();
            txt_MaCTHD.Text = current_cthd.MaCThoadon;
        }

        void Disable_All(bool _disable)
        {
            txt_MaCTHD.ReadOnly = txt_soluong.ReadOnly = _disable;
            cb_laptop.Enabled = !_disable;
        }

        private void txt_soluong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
